package controllers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import play.Play;
import Tools.FileHepler;
import models.BaseModule;

/**
 * Ajax::Ajax主控制器,所有的Ajax都可以写在这里,方便管理
 * 
 * @see BaseModule
 * @since 1.0
 * @author Mr.J.
 * **/
public class Ajax extends Application implements BaseModule{
	public static void addArticleImg(String dir, File imgFile, String localUrl){
		if(!request.contentType.equals("multipart/form-data") 
				&& !dir.equals("image")) return;
		String JSON = "";
		String path = "";
		if(imgFile.canRead() && imgFile.isFile()){
			try {
				File f = FileHepler.createNewFile("ResourceArticleAdmin" + File.separator
						+ getCurrutUser().name + File.separator);
				FileInputStream fin = new FileInputStream(imgFile);
				FileOutputStream fout = new FileOutputStream(f);
				byte[] b = new byte[fin.available()];
				fin.read(b);
				fout.write(b);
				fin.close();
				fout.close();
				path = f.getCanonicalPath();
				path = path.substring(Play.applicationPath.toString().length());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String sep = "/";
			path = path.replace(File.separator, sep);
			JSON = "{\"error\": 0,\"url\":\"" + path + "\"}";
		}
		renderJSON(JSON);
	}
	
	public static void addArticleManager(){
		System.out.println("addArticleManager!!!");
		System.out.println(request.contentType);
		System.out.println(request.querystring);
//		String moveupDirPath = "";
//		String currentDirPath = "";
//		String currentUrl = "";
//		JSONObject result = new JSONObject();
//		result.put("moveup_dir_path", moveupDirPath);
//		result.put("current_dir_path", currentDirPath);
//		result.put("current_url", currentUrl);
//		renderJSON(result);
	}
}
